/**
 * 详情对话框
 */
var LabourModelContentInfoDlg = {
    data: {
        id: "",
        categoryId: "",
        imageUrl: "",
        title: "",
        articleTime: "",
        modelContent: "",
        sortNo: "",
        status: "",
        deleteFlag: "",
        createUser: "",
        createTime: "",
        updateUser: "",
        updateTime: "",
        commendator: "",
        coverCommendator: "",
        enclosure: "",
        enclosureFileName:""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;


    //普通图片上传
    var upload = layui.upload;
    var filePath =new Array();
    upload.render({
        elem: '#urlImg'
        ,accept: 'images'
        ,exts: 'jpg|jpeg|png|docx|pdf'
        ,url: Changein.ctxPath + '/file/uploadFile'
        ,data: {"moduleCode":"TEMPLATE","moduleName":"就创模范内容图片"}
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#filePreview').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            if (res.code==200){
                Changein.success("文件上传成功！"+res.data.src);
                filePath.push(res.data.src);
                $("#imageUrl").val(res.data.src);

                layui.$('#uploadView').removeClass('layui-hide').find('img').attr('src', res.data.src);
            }else {
                Changein.error("文件上传失败！" + res.data.message,5000);
            }
        }
        ,error: function(){
            Changein.error("上传文件失败！");
        }
    });

    //附件上传
    var upload = layui.upload;
    var filePath =new Array();
    upload.render({
        elem: '#enclosureFile'
        ,accept: 'file'
        ,url: Changein.ctxPath + '/file/uploadFile'
        ,data: {"moduleCode":"TEMPLATE","moduleName":"就创模范内容文件"}
        ,before: function(obj){
            obj.preview(function(index, file, result){
                $("#fileview").text(file.name);
            });
        }
        ,done: function(res){
            if (res.code==200){
                Changein.success("文件上传成功！");
                filePath.push(res.data.src);
                $("#enclosure").val(res.data.src);
                layui.$('#uploadView').removeClass('layui-hide').find('img').attr('src', res.data.src);
            }else {
                Changein.error("文件上传失败！" + res.data.message,5000);
            }
        }
        ,error: function(){
            Changein.error("上传文件失败！");
        }
    });

    //渲染时间选择框
    var laydate = layui.laydate;
    laydate.render({
        elem: '#articleTime' //指定元素
    });


    //获取详情信息，填充表单
    var ajax = new $ax(Changein.ctxPath + "/labourModelContent/detail?id=" + Changein.getUrlParam("id"));
    var result = ajax.start();
    form.val('labourModelContentForm', result.data);
    var imgurl = Changein.ctxPath + result.data.imageUrl;
    $('#filePreview').attr('src', imgurl);
    $('#filePreview').css({"width":"100px","height":"100px"});

    $("#fileview").text(result.data.enclosureFileName);
    initContent(result.data.modelContent);
    initCategorySelect(result.data.categoryType);


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Changein.ctxPath + "/labourModelContent/editItem", function (data) {
            Changein.success("更新成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Changein.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //下拉框处理
    form.on('select(categoryType)', function (data) {
        initCategorySelect(data.value);
    });

    //初始化分类下拉
    function initCategorySelect(typeId) {
        $("#categoryId").empty();
        if (typeId  == 0) {
            return;
        }
        layui.use(['form', 'element'], function () {
            var form = layui.form;
            form.render('select');
        });

        var param = {
            type: typeId,
        };

        $.ajax({
            type: "POST",
            url: Changein.ctxPath+"/labourModelCategory/getAllLabourModelCategoryList",
            data: param,
            async:false,
            traditional: true,//这里设置为true
            success: function (result) {
                if (result.code == 0) {
                    result = result.data;
                    $("#categoryId").empty();
                    for (var idx in result) {
                        $("#categoryId").append('<option value=' + result[idx].id + ' class="l-lab-text">' + result[idx].typeName + '</option>');
                    }
                    layui.use(['form', 'element'], function () {
                        var form = layui.form;
                        form.render('select');
                    });
                }
            }
        });
    }

});

function initContent(content) {
    ue = UE.getEditor('editor', {
        enterTag: 'br',
        autoHeightEnabled: false,
        zIndex: 9,
        initialFrameHeight: 400
    });
    if (content) {
        ue.addListener("ready", function () {
            ue.execCommand("insertHtml", content);
            // ue.setDisabled();//设置文本编辑器不可编辑
        });
    }

}